package com.ele.client.activity.home;

import com.ele.client.ClientFactory;
import com.ele.client.activity.auth.LoginPlace;
import com.ele.client.rpc.SimpleAsyncCallback;
import com.ele.client.util.UserHelper;
import com.ele.model.Answer;
import com.ele.model.Section;
import com.ele.model.Word;
import com.github.gwtbootstrap.client.ui.AlertBlock;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class HomeActivity extends AbstractActivity implements HomeView.Presenter {

    private ClientFactory clientFactory;
    private HomeView view;
    private HomePlace place;

    private Section section;

    public HomeActivity(HomePlace place, ClientFactory clientFactory) {
        this.place = place;
        this.clientFactory = clientFactory;
    }

    @Override
    public void start(AcceptsOneWidget container, EventBus eventBus) {
        if (UserHelper.getUser() == null) {
            UserHelper.setReferrer(place);
            clientFactory.getPlaceController().goTo(new LoginPlace(null));
        } else {
            view = clientFactory.getHomeView();
            view.setPresenter(this);
            container.setWidget(view.asWidget());

            loadSection();
        }
    }

    private void loadSection() {
        view.startLoading();

        clientFactory.getApplicationService().getCurrentSection(new SimpleAsyncCallback<Section>() {
            @Override
            public void onSuccess(Section result) {
                if (result != null) {
                    if (section == null || result.getId().equals(section.getId())) {
                        section = result;

                        view.setName(result.getName());

                        view.clearWords();
                        for (Word word : result.getWords()) {
                            final AlertBlock block = new AlertBlock(word.getTranslation());
                            block.setHeading(word.getText());
                            block.setClose(false);
                            block.setType(AlertType.INFO);
                            block.getElement().getStyle().setFloat(Style.Float.LEFT);
                            block.getElement().getStyle().setMarginRight(5, Style.Unit.PX);
                            block.setWidth("120px");
                            view.addWord(block);
                        }

                        view.setDescriptionPlaceholder(result.getRule());
                    }
                    view.setQuestion(result.getCurrentQuestion().getText());
                } else {
                    Window.alert("Вы прошлы весь материал, который доступен на данный момент");
                }

                view.stopLoading();
            }
        });
    }

    @Override
    public void checkAnswer(String answer) {
        view.clearHelp();

        Long questionId = section.getCurrentQuestion().getId();
        clientFactory.getApplicationService().checkAnswer(answer, questionId, new SimpleAsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean correct) {
                if (correct) {
                    loadSection();
                } else {
                    StringBuilder sb = new StringBuilder();

                    sb.append("Варианты правильных ответов:");
                    for (Answer a : section.getCurrentQuestion().getAnswers()) {
                        sb.append("<br >").append(a.getText());
                    }

                    view.showHelp(sb.toString());
                }
            }
        });
    }
}
